import { PermissionsAndroid,Platform} from 'react-native';
import { init, Geolocation} from 'react-native-amap-geolocation';
import axios from 'axios';

class Geo{
    async initGeo() {
        if(Platform.OS === "android"){
            await PermissionsAndroid.request(PermissionsAndroid.PERMISSIONS.ACCESS_COARSE_LOCATION);
        }
        await init({
            ios:"8d702723f03829a4a448281dbb7cd582",
            android:"8d702723f03829a4a448281dbb7cd582"
        });
        return Promise.resolve();
    }
    async getCurrentPosition() {
        return new Promise((resolve,reject)=>{
            console.log('开始定位');
            Geolocation.getCurrentPosition(({coords})=>{
                resolve(coords);
            },reject)
        })
    }
    async getCityByLocation() {
        await init({
            ios:"8d702723f03829a4a448281dbb7cd582",
            android:"8d702723f03829a4a448281dbb7cd582"
        });
        const { longitude, latitude} = await this.getCurrentPosition();
        const res = await axios.get("https://restapi.amap.com/v3/geocode/regeo",{
            params:{ location:`${longitude},${latitude}`,key:"e8f8142a6ce2103f41eb9bc5f61850d2",}
        });
        return Promise.resolve(res.data);
    }
}

export default new Geo();